/*
 * formularioPrincipal.java
 * Generado con NetBeans
 *
 * Created on 12-dic-2009, 14:36:39
 */
package interfazplg;

import codigoPlg.tLeer;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import plg.AnalizadorLexico;

/**
 * Clase principal. Implementa la interfaz gráfica.
 *
 * @author PLG Grupo 6
 */
public class formularioPrincipal extends javax.swing.JFrame {

     private File fichero; //Archivo para cargar el texto

    /**
     * Constructor de la clase.
     * Llama a inicializar componentes.
     */
    private formularioPrincipal() {
        initComponents();
        // por defecto el boton tokenizar está deshabilitado
        this.botonTokenizar.setEnabled(false);
        this.setLocation(200, 200);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TACodigoFuente = new java.awt.TextArea();
        TATokens = new java.awt.TextArea();
        botonTokenizar = new java.awt.Button();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        botonAbrir = new javax.swing.JMenuItem();
        botonGuardar = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Compilador PLg");

        TACodigoFuente.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                TACodigoFuenteKeyReleased(evt);
            }
        });

        TATokens.setEditable(false);

        botonTokenizar.setLabel("Tokenizar");
        botonTokenizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonTokenizarActionPerformed(evt);
            }
        });

        jMenu1.setText("Archivo");

        botonAbrir.setText("Abrir");
        botonAbrir.setToolTipText("Carga el código desde un archivo");
        botonAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAbrirActionPerformed(evt);
            }
        });
        jMenu1.add(botonAbrir);

        botonGuardar.setText("Guardar");
        botonGuardar.setToolTipText("Guarda el código en un archivo");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });
        jMenu1.add(botonGuardar);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ayuda");

        jMenuItem1.setText("Versión");
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(53, 53, 53)
                .add(TACodigoFuente, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 154, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(botonTokenizar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(21, 21, 21)
                .add(TATokens, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                .addContainerGap(77, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(168, 168, 168)
                .add(botonTokenizar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(208, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(81, 81, 81)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, TATokens, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, TACodigoFuente, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE))
                .add(70, 70, 70))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Realiza la acción del botón tokenizar.
     * @param evt
     */
    private void botonTokenizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonTokenizarActionPerformed
        AnalizadorLexico lex =  AnalizadorLexico.getAnalizadorLexico();
        lex.tokenizador(this.TACodigoFuente.getText());
        ArrayList listaTokens = lex.getListaTokens();
        this.TATokens.append(tLeer.leerArray(listaTokens));
    }//GEN-LAST:event_botonTokenizarActionPerformed

    /**
     * Realiza la acción del botón abrir.
     * @param evt
     */
    private void botonAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAbrirActionPerformed
        JFileChooser chooser = new JFileChooser();
        this.fichero = chooser.getCurrentDirectory();
        chooser.setCurrentDirectory(fichero);
        if ((chooser.showOpenDialog(this.botonAbrir)) 
               != JFileChooser.APPROVE_OPTION) {
            return;
        }
        this.fichero = chooser.getSelectedFile();
        String s = fichero.getAbsolutePath();
        this.TACodigoFuente.setText("");
        this.TACodigoFuente.append(tLeer.leerArchivo(fichero));
        this.botonTokenizar.setEnabled(true); //habilita el botón tokenizar
    }//GEN-LAST:event_botonAbrirActionPerformed

    /**
     * Realiza la acción del botón guardar.
     * @param evt
     */
    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        JFileChooser chooser = new JFileChooser();
        File file = chooser.getCurrentDirectory();
        chooser.setCurrentDirectory(file);

        if ((chooser.showSaveDialog(this.botonGuardar))!=JFileChooser.APPROVE_OPTION)return;

        File ficheroSalvar = chooser.getSelectedFile();
        try{
            PrintWriter writer = new PrintWriter(ficheroSalvar);
            writer.print(TACodigoFuente.getText());
            writer.close();
        }
        catch(IOException e){
            System.out.println("Error");
        }
    }//GEN-LAST:event_botonGuardarActionPerformed

    /**
     * Método que comprueba si hay texto introducido en el área de
     * texto de la izquierda y activa el botón tokenizar en tal
     * caso.
     *
     * @param evt
     */
    private void TACodigoFuenteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TACodigoFuenteKeyReleased
        if (this.TACodigoFuente.getText().isEmpty()) {
            this.botonTokenizar.setEnabled(false);
        } else {
            this.botonTokenizar.setEnabled(true);
        }
    }//GEN-LAST:event_TACodigoFuenteKeyReleased

    /**
     * Método principal.
     *
     * @param args
     *      Argumentos de entrada al programa.
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new formularioPrincipal().setVisible(true);
                WindowListener l = new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                    System.exit(0);
                 }
                };
            }
        });
    }

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.TextArea TACodigoFuente;
    private java.awt.TextArea TATokens;
    private javax.swing.JMenuItem botonAbrir;
    private javax.swing.JMenuItem botonGuardar;
    private java.awt.Button botonTokenizar;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    // End of variables declaration//GEN-END:variables

}